import request from '@/utils/request'
import axios from 'axios';
import {
    warnInfo,
    getHost
} from "../../utils/commonUtils.js";

export function getCourseInfo(data) {
    return request({
        url: '/courseCenter/course/getCourseWithChapterAndCourseName',
        method: 'post',
        data
    })
}

export function addCourseMaterrial(data) {
    return request({
        url: '/courseCenter/courseMaterrial/addCourseMaterrial',
        method: 'post',
        data
    })
}

export function getCourseMaterrial(data) {
    return request({
        url: '/courseCenter/courseMaterrial/getCourseMaterrial',
        method: 'post',
        data
    })
}

export function deleteCourseMaterrial(data) {
    return request({
        url: '/courseCenter/courseMaterrial/deleteCourseMaterrial',
        method: 'post',
        data
    })
}

/**
 * 提交课程资料
 * **/
export function submitUploadCourseMaterrial(_this) {
    console.log("开始上传文件");
    if (_this.courseMaterrialList.length > 0) {
        console.log("文件数量为:" + _this.courseMaterrialList.length);
        const formData = new FormData();

        $(_this.courseMaterrialList).each(function(index, val) {
            formData.append("files[" + index + "]", val.raw);
            formData.append("fileNames[" + index + "]", _this.token);
        });
        _this.loading = true;
        _this.progressSignal = true;
        //上传文件
        var stopSignal = false;
        _this.uploadPercentage = 0;
        var storageSignal = false;
        axios({
                method: "POST",
                url: getHost() + "/fileCenter/file/uploadCourseMaterrials",
                data: formData, //参数
                onUploadProgress: (progressEvent) => {
                    _this.uploadPercentage =
                        (progressEvent.loaded / progressEvent.total) * 100;
                    _this.uploadPercentage = parseInt(_this.uploadPercentage);
                    if (_this.uploadPercentage >= 100) {
                        storageSignal = true;
                        _this.progressSignal = false;
                        _this.uploadText = "文件上传结束，正在保存中，请稍等...";
                    }
                },
            })
            .then((response) => {
                stopSignal = true;
                _this.loading = false;
                _this.materrialInfo.fileInfo = response.data.data;
                _this.addCourseMaterrial();
                _this.$refs["upload"].clearFiles();
            })
            .catch((err) => {});
        //开始执行定时器获取上传进度
        // var timer = window.setInterval(() => {
        //     setTimeout(() => {
        //         if (storageSignal) {
        //             $.ajax({
        //                 url: getHost() + "/fileCenter/file/getUploadProgress",
        //                 data: {
        //                     sign: "uploadCourseMaterrials",
        //                 },
        //                 dataType: "json",
        //                 type: "post",
        //                 //加上这句话,允许浏览器向服务器跨域请求时携带cookie-保证sessionID一致
        //                 xhrFields: {
        //                     withCredentials: true,
        //                 },
        //                 crossDomain: true,
        //                 beforeSend: function(request) {
        //                     request.setRequestHeader(_this.tokenKey, _this.token);
        //                 },
        //                 success: function(data) {
        //                     var progress = parseInt(data.data);
        //                     _this.uploadPercentage = progress;
        //                     if (progress >= 100 || stopSignal) {
        //                         _this.uploadPercentage = 100;
        //                         _this.progressSignal = false;
        //                         _this.uploadPercentage = 0;
        //                         //停止进行
        //                         clearInterval(timer);
        //                     }
        //                 },
        //                 error: function(data) {
        //                     clearInterval(timer); //停止进行
        //                 },
        //             });
        //         }
        //     }, 0);
        // }, 300);
    } else {
        warnInfo(_this, "您未选择任何文件");
    }
}